<template>
  <el-dialog
     v-drag
    :title="!dataForm.storeId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :inline="true" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="storeId">
      <el-input size="mini"  v-model="dataForm.storeId" placeholder="" clearable></el-input>
    </el-form-item>
                                                                                                                    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          storeId: 0,
          storeCode: '',
          companyId: '',
          companyNo: '',
          companyFullName: '',
          storType: '',
          storeName: '',
          storeFullName: '',
          contactor: '',
          tel: '',
          mobile: '',
          province: '',
          city: '',
          zone: '',
          storeAddress: '',
          shopTime: '',
          cardNo: '',
          cardPic: '',
          longitude: '',
          latitude: '',
          createTime: '',
          updateTime: '',
          createUsername: '',
          createUserId: '',
          updateUsername: '',
          updateUserId: '',
          status: '',
          isDeleted: '',
          remark: ''
        },
        dataRule: {
          storeCode: [
            { required: true, message: '门店编码不能为空', trigger: 'blur' }
          ],
          companyId: [
            { required: true, message: '批发企业Id不能为空', trigger: 'blur' }
          ],
          companyNo: [
            { required: true, message: '批发企业名称不能为空', trigger: 'blur' }
          ],
          companyFullName: [
            { required: true, message: '所在批发企业名称不能为空', trigger: 'blur' }
          ],
          storType: [
            { required: true, message: '门店类别 0:单体，1:连锁不能为空', trigger: 'blur' }
          ],
          storeName: [
            { required: true, message: '门店名称不能为空', trigger: 'blur' }
          ],
          storeFullName: [
            { required: true, message: '门店全称不能为空', trigger: 'blur' }
          ],
          contactor: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '电话/(客服电话)不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机不能为空', trigger: 'blur' }
          ],
          province: [
            { required: true, message: '省名称不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '市名称不能为空', trigger: 'blur' }
          ],
          zone: [
            { required: true, message: '区名称不能为空', trigger: 'blur' }
          ],
          storeAddress: [
            { required: true, message: '营业地址不能为空', trigger: 'blur' }
          ],
          shopTime: [
            { required: true, message: '营业时间不能为空', trigger: 'blur' }
          ],
          cardNo: [
            { required: true, message: '证件号码不能为空', trigger: 'blur' }
          ],
          cardPic: [
            { required: true, message: '证件图片不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          latitude: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          createUsername: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          updateUsername: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          updateUserId: [
            { required: true, message: '修改人id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '是否可用 1:是不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除 1:是不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.storeId = id || 0
        this.visible = true
        this.$nextTick(async () => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.storeId) {
            const res = await this.$http({
              url: `/base/basestoreinfo/info/${this.dataForm.storeId}`,
              method: 'get',
              params: this.$http.adornParams()
            })
                let data = res.data
                if (data && data.code === 0) {
                    this.dataForm = data.baseStoreInfo
                }
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate(async (valid) => {
          if (valid) {
            const res = await this.$http({
              url:`/base/basestoreinfo/${!this.dataForm.storeId ? 'save' : 'update'}`,
              method: 'post',
             data: this.$http.adornData( this.dataForm)
            })
            let data = res.data
            if (data && data.code === 0) {
                this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                }
            })
            } else {
                this.$message.error(data.msg)
            }
          }
        })
      }
    }
  }
</script>
